package com.roamer.javase.demo.algorithm;

import lombok.Data;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 菜单树
 *
 * @author roamer
 * @version v1.0
 * @date 2019/11/6 10:27
 */
@Data
public class MenuTree implements Serializable {
    private static final long serialVersionUID = 567309075214644556L;

    private Node rootNode = new Node().setId(0L).setName("菜单").setMenuType(MenuType.ROOT_MENU);

    private Map<Long, Node> map = new HashMap<>();

    {
        map.put(0L, rootNode);
    }

    public Node getMenu(Long id) {
        return map.get(id);
    }

    /**
     * 菜单
     */
    public class Node {

        private Long id;

        private String name;

        private MenuType menuType;

        List<Node> childNodes = new ArrayList<>();

        public void addChildNode(Node node) {
            childNodes.add(node);
            map.put(node.id, node);
        }

        public Long getId() {
            return id;
        }

        public Node setId(Long id) {
            this.id = id;
            return this;
        }

        public String getName() {
            return name;
        }

        public Node setName(String name) {
            this.name = name;
            return this;
        }

        public MenuType getMenuType() {
            return menuType;
        }

        public Node setMenuType(MenuType menuType) {
            this.menuType = menuType;
            return this;
        }

        public List<Node> getChildNodes() {
            return childNodes;
        }

        public Node setChildNodes(List<Node> childNodes) {
            this.childNodes = childNodes;
            return this;
        }
    }


    /**
     * 菜单类型
     */
    public enum MenuType {
        ROOT_MENU,
        MENU,
        DEVICE,
        ;
    }
}
